package com.module.user.ui.version

import com.common.app.base.AppActivity
import com.common.app.data.api.ApiConstant.PRIVACY_POLICY_SERVER
import com.common.app.data.api.ApiConstant.SERVICE_AGREEMENT_SERVER
import com.common.app.data.bean.VersionResBean
import com.common.app.helper.AppConfigHelper
import com.common.app.helper.LaunchHelper
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.*
import com.common.base.utils.AppUtils
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.VersionUtils
import com.common.base.utils.getOnlyDigits
import com.module.user.R
import com.module.user.databinding.UserActivityVersionBinding
import com.common.app.utls.ApkDownLoadUtils

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/10/13
 * <p>
 * 描述：版本信息页面
 * <p>
 * 修订历史：
 */
class VersionActivity : AppActivity<UserActivityVersionBinding, VersionViewModel>() {

    override fun getViewModel() = VersionViewModel::class.java
    override fun getViewContentBinding() = UserActivityVersionBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    private var version: VersionResBean? = null

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.ac_about_title))
        headTitleUtils.setLineStyle(10)
    }

    override fun initEvents() {
        super.initEvents()
        mViewContentBinding.versionUpdate.setOnClickListener {
            showDownloadDialog(version)
        }
        //用户协议
        mViewContentBinding.protocolLayout1.setOnClickListener {
            LaunchHelper.launchWebActivity(
                SERVICE_AGREEMENT_SERVER
            )
        }
        //隐私协议
        mViewContentBinding.protocolLayout2.setOnClickListener {
            LaunchHelper.launchWebActivity(
                PRIVACY_POLICY_SERVER
            )
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()

//        setText(mViewContentBinding.appNameTv, getString(R.string.app_name))
        setText(
            mViewContentBinding.aboutVcodeTv,
            getString(
                R.string.common_vcode_hint,
                VersionUtils.getVerName(AppUtils.getContext()) + "-" + VersionUtils.getVerCode(AppUtils.getContext())
            )
        )

//        mViewModel.getVersionLast().observe(this) {
//            it.isSuccess().yes { it.data?.let { it1 -> update(it1) } }
//        }

        requestVersion()
    }

    private fun update(it: VersionResBean) {
        version = it

        (getOnlyDigits(VersionUtils.getVerName(this),false) >= getOnlyDigits(it.versionName?:"0",false)).no {
            mViewContentBinding.versionGetversion.gone()
            mViewContentBinding.versionUpdate.visible()
            val apkDownloadHint = AppConfigHelper.getApkDownloadHint()
            apkDownloadHint.no { showDownloadDialog(it) }
        }.otherwise {mViewContentBinding.versionUpdate.gone()
            mViewContentBinding.versionGetversion.visible()
        }
    }

    private fun showDownloadDialog(it: VersionResBean? = null) {
        val download = ApkDownLoadUtils(this, it)
        download.showDialog(supportFragmentManager)
    }

    private fun requestVersion() {
        showDialogLoading()
        mViewModel.requestVersion().observe(this) {
            dismissLoadingDialog()
            it.isSuccess().yes {
                it.Data?.let { it1 ->
                    update(it1)
                }
            }.otherwise {
            }
        }
    }
}